<?php
include "header.php";
if($_COOKIE['userrole']!="1")
{
    die("admin need");
}
?>

<div class="container-fluid">
    <!--      <div class="row">-->
    <!--        <div class="col-sm-3 col-md-2 sidebar">-->
    <!--          <ul class="nav nav-sidebar">-->
    <!--            <li class="active">-->
    <!--                <a href="#">公云<span class="sr-only">(current)</span></a>-->
    <!--            </li>-->
    <!--            <li><a href="#">熙地港</a></li>-->
    <!--            <li><a href="#">望京凯德</a></li>-->
    <!--          </ul>-->
    <!---->
    <!--        </div>-->
    <div class="col-sm-12  col-md-12  main">
        <h1 class="page-header">行业
            <button id="btn_cancelCreateIndu" type="button" class="btn btn-lg btn-warning pull-right" onclick="cancelCreateIndu()" style="display: none">取消</button>
            <button id="btn_finish" type="button" class="btn btn-lg btn-success pull-right" onclick="finishCreateIndu()" style="display: none">保存</button>
            <button id="btn_modify" type="button" class="btn btn-lg btn-success pull-right" onclick="finishModifyIndu()" style="display: none">修改</button>
<!--            <button id="btn_create" type="button" class="btn btn-lg btn-success pull-right" onclick="showCreateInduDiv()">创建</button>-->

        </h1>

        <div style="padding: 100px 100px 10px;display: none" id="div_createIndu">
            <form class="bs-example bs-example-form" role="form">

                <div class="input-group">
                    <span class="input-group-addon">行业名称</span>
                    <input id="input_name" type="text" class="form-control" placeholder="请输入行业名称" onblur="checkWidgetEcho(this.id,'indu','name',this.value,current_modifyindu_id,'行业名')">
                </div>
                <br>



            </form>
        </div>

        <div class="table-responsive">
            <table class="table table-striped " id="induTable" >

            </table>
        </div>
    </div>
</div>
</div>


<script>
    var tableurl="api.php?method=getIndusTable";
    var $table = $('#induTable');
    $table.bootstrapTable({
        //url: "merchantTable.json",
        url:tableurl,
        dataType: "json",
        pagination: true, //分页
        singleSelect: false,
        search: true, //显示搜索框
        pageSize:10,
        columns: [
            {
                title: 'ID',
                field: 'id',
                align: 'left',
                valign: 'middle'
            },
            {
                title: '行业名',
                field: 'name',
                align: 'left',
                valign: 'middle'
            },
            {
                title: '子行业',
                field: 'childnames',
                align: 'left',
                valign: 'middle'
            },
            {
                title: '操作',
                field: 'id',
                align: 'left',
                formatter:function(value,row,index){
                    s=e=d="";
                    e='<a href="#" mce_href="#" onclick="editIChild(\''+ row.id + '\',\''+row.name+'\',\''+row.childnames+'\')">编辑子行业 </a>'

                    //d = '<a href="#" mce_href="#" onclick="delI(\''+ row.id +'\')">删除</a> ';

                    return s+e+d;
                }
            }
        ]
    });


    $('.cls_select2').select2({width:"100%"});
    $('.cls_select2_50').select2({width:"50%"});
    $('.cls_select2_mt').select2({width:"100%",multiple: true});


    function editIChild(id,name,childname)
    {
        bootbox.prompt(name+"已有子行业：<font color='red'>"+childname+"</font>.请在下方输入一个新增的子行业！",function (result) {
            if(result==null)return;
            if(result!="")
            {
                $.ajax({
                    url:'api.php?method=addChildIndustry',
                    type:'POST', //GET
                    async:true,    //或false,是否异步
                    data:{
                        pid:id,
                        childname:result
                    },
                    timeout:5000,    //超时时间
                    dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
                    beforeSend:function(xhr){
                        console.log(xhr)
                        console.log('发送前')
                    },
                    success:function(data,textStatus,jqXHR){
                        //alert(data.name);
                        if(data.return_code=="0")
                        {

                            bootbox.alert("添加子行业成功",function () {

                                $("#induTable").bootstrapTable('refresh',{'url':tableurl});
                            });
                        }
                        else
                        {
                            alert("添加子行业失败")
                        }



                    },
                    error:function(xhr,textStatus){
                        console.log('错误')
                        console.log(xhr)
                        console.log(textStatus)
                    },
                    complete:function(){
                        console.log('结束')
                    }
                });
            }
        })
    }


    current_modifyindu_id=0;
    function editI(id,name)
    {
        //leak floor_info, ,indus
        current_modifyindu_id = id;
        $("#input_name").val(name);

        showModifyInduDiv();
    }


    function delR(rid)
    {
        bootbox.prompt("<font color='red'>确定要删除此行业吗？</font>（此操作不可撤销！如需删除请在下方输入“确认删除”）",function (result) {
            if(result=="确认删除")
            {
                $.ajax({
                    url:'api.php?method=delIndu',
                    type:'POST', //GET
                    async:true,    //或false,是否异步
                    data:{
                        indu_id:rid
                    },
                    timeout:5000,    //超时时间
                    dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
                    beforeSend:function(xhr){
                        console.log(xhr)
                        console.log('发送前')
                    },
                    success:function(data,textStatus,jqXHR){
                        //alert(data.name);
                        if(data.return_code=="0")
                        {

                            bootbox.alert("删除成功",function () {

                                $("#induTable").bootstrapTable('refresh',{'url':tableurl});
                            });
                        }
                        else
                        {
                           alert("删除失败")
                        }



                    },
                    error:function(xhr,textStatus){
                        console.log('错误')
                        console.log(xhr)
                        console.log(textStatus)
                    },
                    complete:function(){
                        console.log('结束')
                    }
                });
            }
        })
    }

    function showCreateInduDiv()
    {
        $("#div_createIndu").show();
        $("#btn_cancelCreateIndu").show();
        $("#btn_finish").show();
        $("#btn_create").hide();
        current_modifyindu_id=0;
        $(".table-responsive").hide();
    }

    function cancelCreateIndu()
    {
        $("#div_createIndu").hide();
        $("#btn_cancelCreateIndu").hide();
        $("#btn_finish").hide();
        $("#btn_modify").hide();
        $("#btn_create").show();

        $("#input_name").val("");

        $(".table-responsive").show();

    }

    function showModifyInduDiv()
    {
        $("#div_createIndu").show();
        $("#btn_cancelCreateIndu").show();
        $("#btn_modify").show();
        $("#btn_create").hide();
        $(".table-responsive").hide();



    }

    function checkInduInput()
    {
        name = $("#input_name").val();



        if(name=="")
        {
            bootbox.alert("行业名称不能为空");
            return false;
        }


        //alert(industrydetail);
        return true;
    }

    function finishModifyIndu()
    {
        bcheck=checkInduInput();
        if(!bcheck)return;
        bootbox.confirm("确定修改此行业的信息吗？",function (result) {

            if(result)
                $.ajax({
                    url:'api.php?method=modifyIndu',
                    type:'POST', //GET
                    async:true,    //或false,是否异步
                    data:{
                        id:current_modifyindu_id,
                        name:name

                    },
                    timeout:5000,    //超时时间
                    dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
                    beforeSend:function(xhr){
                        console.log(xhr)
                        console.log('发送前')
                    },
                    success:function(data,textStatus,jqXHR){
                        //alert(data.name);
                        if(data.return_code=="0")
                        {
                            bootbox.alert("修改成功",function () {
                                cancelCreateIndu();
                                $("#induTable").bootstrapTable('refresh',{'url':tableurl});
                            });
                        }
                        else
                        {
                            alert(data.return_msg);
                        }



                    },
                    error:function(xhr,textStatus){
                        console.log('错误')
                        console.log(xhr)
                        console.log(textStatus)
                    },
                    complete:function(){
                        console.log('结束')
                    }
                });
        })

    }

    function finishCreateIndu()
    {
        bcheck=checkInduInput();
        if(!bcheck)return;

        //return;

        $.ajax({
            url:'api.php?method=addIndu',
            type:'POST', //GET
            async:true,    //或false,是否异步
            data:{
                name:name


            },
            timeout:5000,    //超时时间
            dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
            beforeSend:function(xhr){
                console.log(xhr)
                console.log('发送前')
            },
            success:function(data,textStatus,jqXHR){
                //alert(data.name);
                if(data.return_code=="0")
                {
                    bootbox.alert("创建成功",function () {
                        cancelCreateIndu();
                        $("#induTable").bootstrapTable('refresh',{'url':tableurl});
                    });
                }
                else
                {
                    if(data.return_code=="-3")
                    {
                        bootbox.alert("行业名重复，请重新输入！");
                        return;
                    }

                }



            },
            error:function(xhr,textStatus){
                console.log('错误')
                console.log(xhr)
                console.log(textStatus)
            },
            complete:function(){
                console.log('结束')
            }
        });

    }


</script>
</body>
</html>
